package zl.osimall.front.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import zl.osimall.common.bean.Goods;
import zl.osimall.common.exception.SteduException;
import zl.osimall.common.service.GoodsService;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/search")
    public String search(Integer categoryId, String name,Integer currentPage , Model m){
        Goods condition = new Goods();
        condition.setCategoryId(categoryId);
        condition.setName(name);
        //List<Goods> goodsList = goodsService.selectCondition(condition);
        if(currentPage == null){
            currentPage = 1;
        }
        PageInfo<Goods> goodsList = goodsService.selectByLike(condition, currentPage, 4);
        System.out.println(goodsList.toString());
        m.addAttribute("goodsList", goodsList);
        m.addAttribute("categoryId", categoryId);
        m.addAttribute("name", name);
        return "search";
    }

    @GetMapping("/{goodsId}")
    public String selectById(@PathVariable("goodsId") Integer goodsId, Model m) throws SteduException {
        Goods goods = goodsService.selectById(goodsId);
        m.addAttribute("goods", goods);
        return "goods";
    }
}
